package com.tbeasy.financial.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Util {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
	private static SimpleDateFormat yearDateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public static String formatNumber(String number) {
		NumberFormat numberFormat = NumberFormat.getNumberInstance();
		numberFormat.setMaximumFractionDigits(2);
		
		return numberFormat.format(Double.valueOf(number));
		
	}
	
	public static long getCurrentMonthTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTimeInMillis();
	}
	
	public static long getFirstDayOfMonth(int month) {
		if(month<0 || month >11) {
			throw new RuntimeException("month must between 1 and 12");
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.MONTH, month);
		return calendar.getTimeInMillis();
	}
	
	public static long formatTime(String string) {
		return 0;
	}
	
	public static long getTodayFirstTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTimeInMillis();
	}
	
	public static long getTodayLastTime() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTimeInMillis();
	}
	
	public static String getWeekValue() {
		
		String beginDay = dateFormat.format(getMonday(new Date()));
		String endDay = dateFormat.format(getSaturday(new Date()));
		return beginDay + "-" + endDay;
	}

	public static String getMonthValue() {
		String beginDay = dateFormat.format(getMonthFirstDay(new Date()));
		String endDay = dateFormat.format(getMonthLastDay(new Date()));
		return beginDay + "-" + endDay;
	}

	public static Date getMonday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DATE, (dayOfWeek - 1) * (-1));
		return cal.getTime();
	}

	public static Date getSaturday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
		cal.add(Calendar.DATE, 7 - dayOfWeek);
		return cal.getTime();
	}

	public static Date getMonthFirstDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
		cal.add(Calendar.DATE, (dayOfMonth - 1) * (-1));
		return cal.getTime();
	}

	public static Date getMonthLastDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
		cal.add(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH) - dayOfMonth);
		return cal.getTime();
	}
	
	public static String formatDate(Date date) {
		return yearDateFormat.format(date);
	}
	
	public static String formatDate(long time) {
		return yearDateFormat.format(new Date(time));
	}
	
	public static Date formatStringToDate(String content) {
		Date date = null;
		try {
			date = yearDateFormat.parse(content);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return date;
	}
}
